#include "sk_triangle_shape.h"
#include "sk_creation_params.h"

#include <QDomDocument>
#include <QDomElement>
#include <QDebug>

#include <Box2D.h>

namespace sk
{

    TriangleShape::TriangleShape()
    {
    }

    int TriangleShape::type()
    {
        return IT_SIMPLE_TRIANGLE;
    }

    GameShape * TriangleShape::clone()
    {
        TriangleShape * res = new TriangleShape();
        res->bodyDef = bodyDef;
        res->fixtureDef = fixtureDef;
        res->dx0 = dx0;
        res->dy0 = dy0;
        res->dx1 = dx1;
        res->dy1 = dy1;
        return res;
    }

    b2Body * TriangleShape::addToWorld(b2World * world)
    {
        int sg = dx0 * dy1 - dx1 * dy0;
        if (0 == sg)
        {
            qDebug() << "degenerate shape";
            return 0;
        }
        b2BodyDef bd = bodyDef;
        bd.angle = bd.angle * DEGREES_TO_RAD;
        bd.position.Set(bd.position.x * PIXEL_TO_METERS, bd.position.y * PIXEL_TO_METERS);
        b2Body * body = world->CreateBody(&bd);

        b2PolygonShape s;

        b2Vec2 vertices[3];
        vertices[0].Set(0.0f, 0.0f);

        if (sg < 0)
        {
            vertices[2].Set(dx0 * PIXEL_TO_METERS, dy0 * PIXEL_TO_METERS);
            vertices[1].Set(dx1 * PIXEL_TO_METERS, dy1 * PIXEL_TO_METERS);
        }
        else
        {
            vertices[1].Set(dx0 * PIXEL_TO_METERS, dy0 * PIXEL_TO_METERS);
            vertices[2].Set(dx1 * PIXEL_TO_METERS, dy1 * PIXEL_TO_METERS);
        }
        int32 count = 3;

        s.Set(vertices, count);

        fixtureDef.shape = &s;
        body->CreateFixture(&fixtureDef);
        return body;
    }

    void TriangleShape::setData(void * params)
    {
        SimpleTriangleParam * p = static_cast<SimpleTriangleParam *>(params);
        dx0 = p->dx0;
        dy0 = p->dy0;
        dx1 = p->dx1;
        dy1 = p->dy1;
    }


    void TriangleShape::serialiseCustomShapeData(QDomDocument * doc, QDomElement * e)
    {
        QDomElement customShapeElement = doc->createElement("customShapeData");
        customShapeElement.setAttribute("dx0", dx0);
        customShapeElement.setAttribute("dy0", dy0);
        customShapeElement.setAttribute("dx1", dx1);
        customShapeElement.setAttribute("dy1", dy1);
        e->appendChild(customShapeElement);
    }

    bool TriangleShape::deserialiseCustomShapeData(QDomElement * e)
    {
        for (int i = 0; i < e->childNodes().count(); ++i)
        {
            QDomElement ee = e->childNodes().at(i).toElement();
            if (ee.tagName() == "customShapeData")
            {
                dx0 = ee.attribute("dx0").toFloat();
                dy0 = ee.attribute("dy0").toFloat();
                dx1 = ee.attribute("dx1").toFloat();
                dy1 = ee.attribute("dy1").toFloat();
                return true;
            }
        }
        return false;
    }

    QString TriangleShape::xmlType()
    {
        return TriangleShape::XmlType();
    }

    QString TriangleShape::XmlType()
    {
        return "triangleShape";
    }

} // namespace sk
